package com.fanfanwang.ffw.app.ui.login;

import java.net.URLDecoder;
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.fanfanwang.ffw.app.AppConstant;
import com.fanfanwang.ffw.app.AppManager;
import com.fanfanwang.ffw.app.MainActivity;
import com.fanfanwang.ffw.app.R;
import com.fanfanwang.ffw.app.base.BaseActivity;
import com.fanfanwang.ffw.app.bean.DianListBean;
import com.fanfanwang.ffw.app.bean.LoginInfoBean;
import com.fanfanwang.ffw.app.common.StringUtils;
import com.fanfanwang.ffw.app.common.ToastAlone;
import com.fanfanwang.ffw.app.asynctask.LoginAsyncTask;
import com.fanfanwang.ffw.app.tools.SharedPreferencesUtil;
import com.fanfanwang.ffw.app.widget.CommonTitleBar;

/** 登录 */
public class LoginAct extends BaseActivity {

	private CommonTitleBar commonTitleBar;
	private EditText lo_mobile;
	private EditText lo_password;
	private TextView tv_forgetpassword;
	private TextView tv_reg_account;
	private Button lo_btn_complete;
	private String mobile = "";
	private String password = "";
	private Activity mAct;
	private Intent mIntent;
	private boolean isNeedIntent= false;//是否需要跳转，如果是退出来，然后进入的登陆，是需要跳转的。

	public static IGetLoginDate iGetLoginDate;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initView();
		AppManager.getAppManager().addActivity(this);
		initData();
		setListener();
	}

	@Override
	public void initView() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_login);
		mAct = this;
		commonTitleBar = (CommonTitleBar) findViewById(R.id.common_titlebar);
		lo_btn_complete = (Button) findViewById(R.id.lo_btn_complete);
		lo_mobile = (EditText) findViewById(R.id.lo_mobile);
		lo_password = (EditText) findViewById(R.id.lo_password);
		tv_forgetpassword = (TextView) findViewById(R.id.tv_forgetpassword);
		tv_reg_account=(TextView)findViewById(R.id.tv_reg_account);
		mIntent=getIntent();
		isNeedIntent=mIntent.getBooleanExtra("isNeedIntent", false);
		
	}

	public void setInterface(){
		iGetLoginDate=new IGetLoginDate() {
			@Override
			public void setLoginData(LoginInfoBean loginInfo) {
				//TODO  然后把数据传过去
				try{
				if(!TextUtils.isEmpty(loginInfo.userid)){
					AppConstant.user_id=loginInfo.userid;
					String user_id = URLDecoder.decode(loginInfo.userid,"utf-8");
					SharedPreferencesUtil.getInstance(mAct).putUserId(user_id);
				}if(!TextUtils.isEmpty(loginInfo.username))
				{
					AppConstant.username=loginInfo.username;
					String username = URLDecoder.decode(loginInfo.username,"utf-8");
					SharedPreferencesUtil.getInstance(mAct).putUsername(username);
				}if(!TextUtils.isEmpty(loginInfo.carnumber))
				{
					AppConstant.carnumber=loginInfo.carnumber;
					String carnumber = URLDecoder.decode(loginInfo.carnumber,"utf-8");
					SharedPreferencesUtil.getInstance(mAct).putCarnumber(carnumber);
				}if(!TextUtils.isEmpty(loginInfo.truename))
				{
					AppConstant.truename=loginInfo.truename;
					String truename = URLDecoder.decode(loginInfo.truename,"utf-8");
					SharedPreferencesUtil.getInstance(mAct).putTruename(truename);
				}
				}catch(Exception e)
				{
					e.printStackTrace();
				}
				if(isNeedIntent)//如果需要跳转，那就跳转并且重置一下变了
				{
				Intent intent =new Intent();
				intent.setClass(LoginAct.this, MainActivity.class);
				startActivity(intent);
				}
				isNeedIntent=false;
				finish();
			}
		};
	}
	@Override
	public void setListener() {
		// TODO Auto-generated method stub
		lo_btn_complete.setOnClickListener(new OnClickListener() {

			@SuppressLint("NewApi")
			@Override
			public void onClick(View v) {
				mobile = lo_mobile.getText().toString();
				password = lo_password.getText().toString();
				// TODO Auto-generated method stub
				if (mobile.isEmpty() || !StringUtils.isMobileNO(mobile)) {
					ToastAlone.showToast(LoginAct.this,
							getString(R.string.login_error_tel),
							Toast.LENGTH_SHORT);
				}else if(TextUtils.isEmpty(password)){
					ToastAlone.showToast(LoginAct.this,
							getString(R.string.login_error_pwd),
							Toast.LENGTH_SHORT);
				} else {
					// 验证登录信息
					new LoginAsyncTask(mAct,mobile,password).execute("");
					//					mIntent = new Intent(mAct, HomeActivity.class);
					//					startActivity(mIntent);
				}
			}
		});
		tv_reg_account.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mIntent = new Intent(mAct, RegisterAct.class);
				startActivity(mIntent);
			}
		});
		tv_forgetpassword.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mIntent = new Intent(mAct, FindPwdAct.class);
				startActivity(mIntent);
			}
		});

	}

	@Override
	public void initData() {
		// TODO Auto-generated method stub
		commonTitleBar.setCenterText(getString(R.string.login));
		setInterface();
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			// do something...
			// mIntent = new Intent(mAct, GuideAct.class);
			// startActivity(mIntent);
			// finish();
		}
		return super.onKeyDown(keyCode, event);
	}
	public interface IGetLoginDate{
		public void setLoginData(LoginInfoBean loginInfo);
	}

}
